package language.advanced.reflect.dynamicProxy.cglibTest;

import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import org.junit.Test;

import java.util.HashMap;

public class Client {
    public static void main(String[] args) {
        // 代理类class文件存入本地磁盘方便我们反编译查看源码
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\code");

        // 通过CGLIB动态代理获取代理对象
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(HelloService.class);
        enhancer.setCallback(new MyMethodInterceptor());
        HelloService proxy= (HelloService)enhancer.create();

        // 通过代理对象调用目标方法
        proxy.sayHello();
    }

    @Test
    public void testMap(){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(HashMap.class);
        enhancer.setCallback(new MyMethodInterceptor());
        HashMap<String, String> proxy= (HashMap<String, String>)enhancer.create();
        proxy.put("A","B");
        System.out.println(proxy);
    }
}